package com.tkmybatis.controller;

import com.tkmybatis.annotation.NoRequestSubmit;
import com.tkmybatis.entity.ECTCNProduct;
import com.tkmybatis.hfy.base.entity.PageBean;
import com.tkmybatis.hfy.base.entity.Result;
import com.tkmybatis.service.ECTCNProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project_name: tk_mybatis_demo
 * Company: 新致软件
 * Author: ZHU YU WEI
 * Date: Created in 22:43 2021/4/5.
 * Description:
 * Modified By:
 */
@RequestMapping("/product")
@RestController
@Slf4j
public class ProductController {


    @Autowired
    private ECTCNProductService ectcnProductService;



    @PostMapping("/insert")
    public Result addECTCNProduct(ECTCNProduct eCTCNProduct){
       return ectcnProductService.addECTCNProduct(eCTCNProduct);
    }

    @RequestMapping("/query")
    public List<ECTCNProduct> queryListByLike(String productName,String productPrice){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("product_name",productName);
        map.put("product_price",Double.parseDouble(productPrice));
       return ectcnProductService.queryListByLike(map);
    }

    @RequestMapping("/page")
    public  PageBean<ECTCNProduct> queryListMapPageByLike(String productName,String productPrice,String pageNum,String pageSize)  {
        Map<String,Object> map= null;
        try {
            map = new HashMap<String,Object>();
            map.put("productName",productName);
            map.put("productPrice",Integer.parseInt(productPrice));
            PageBean<ECTCNProduct> ectcnProductPageBean = ectcnProductService.queryListPageByLike2(map, pageNum, pageSize);
            return ectcnProductPageBean;
        } catch (Exception e) {
            log.info("分页失败",e);
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/count")
    public Integer findCount(String productName,String productPrice){
        //List<Integer> ints=new ArrayList<Integer>();
        Map<String,Object> map = new HashMap<String,Object>();
        map = new HashMap<String,Object>();
        map.put("productName",productName);
        map.put("productPrice",Integer.parseInt(productPrice));
        Integer count = ectcnProductService.findCount(map);
      //  ints.add(count);
        //ints.add(count);
       // ints.add(count);
        return count;
    }


    @RequestMapping("/forMap")
    @Cacheable(value = "queryListMap")
    @NoRequestSubmit
    public List<Map<String,Object>> queryListMap (String productName,String productPrice){
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            map = new HashMap<String,Object>();
            map.put("productName",productName);
            map.put("productPrice",Integer.parseInt(productPrice));
             return ectcnProductService.queryListMap(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
